var islogin,username,sid,apiurl,homedir;
	// 我想骂人，这里居然用固定IP地址
	apiurl='/';//如果是同个ip或则同个端口可以直接使用/,如果是不同ip或者不同端口，填入服务器网址，需要/结尾
	homedir="/RpaWork";//首页的路径
	islogin=false;
	sid='';
	username = $.cookie('user_name');
	sid = $.cookie('sid');
	if(sid =='' || sid == 'undefined' || sid == null){
		islogin=false;
	}else{
		islogin=true;
	}
	console.log(islogin);
    //加载弹出层
    layui.use(['layer','element'],
		function() {
			layer = layui.layer;
			element = layui.element;
			
			element.on('tab(xbs_tab)', function(data){
				const name= this.getAttribute('lay-id');
			  $(".layui-nav-tree .layui-nav-child dd").removeClass('layui-this');
			  $('a[data-name="'+ name +'"]').parent().addClass('layui-this');
			  //if(name=="tasklog"){
			        let pagesrc=$('a[data-name="'+ name +'"]').attr('data-href');
			        console.log(pagesrc);
			        $('iframe[tab-id="'+ name +'"]').attr('src', pagesrc);
			 //}
			});
		  //监听导航点击
		  element.on('nav(leftnavtree)', function(elem){
			  const title=elem.text();
			  const name=this.getAttribute('data-name');
			  const url=this.getAttribute('data-href');
			  if(name != null){
				tab.tabAdd(title,url,name);
				tab.tabChange(name);
			  }
			event.stopPropagation();
			 CustomRightClick();
		  });
    });
	
	  var tab = {
			tabAdd: function(title,url,id,is_refresh=false){
				//重复点击
				for (var i = 0; i <$('.x-iframe').length; i++) {
					if($('.x-iframe').eq(i).attr('tab-id')==id){
						tab.tabChange(id);
						event.stopPropagation();
						if(is_refresh)$('.x-iframe').eq(i).attr("src",$('.x-iframe').eq(i).attr('src'));
						return;
					}
				};
			  //新增一个Tab项
			  element.tabAdd('xbs_tab', {
				title: title 
				,content: '<iframe tab-id="'+id+'" frameborder="0" src="'+url+'" scrolling="auto" class="x-iframe"></iframe>'
				,id: id
			  })
			}
			,tabDelete: function(othis){
			   element.tabDelete('xbs_tab',othis);
			}
			,tabChange: function(id){
			  //切换到指定Tab项
			  element.tabChange('xbs_tab', id);

			}
			,closeother: function() {  
				$('.layui-tab-title li').each(function(i,el){
				  var id = $(el).attr('lay-id');
				  if(!$(this).hasClass('layui-this') && id!=''){
					element.tabDelete('xbs_tab', id);
				  }
				   
				});
			  
			}
			,closeall: function(t) {  
				 $('.layui-tab-title li').each(function(){
					var id = $(this).attr('lay-id');
					element.tabDelete('xbs_tab', id);
				 })
				 $('.layui-tab-title li').eq(0).addClass('layui-this');            
			  },
		  };
			tableCheck = {
				init:function  () {
					$(".layui-form-checkbox").click(function(event) {
						if($(this).hasClass('layui-form-checked')){
							$(this).removeClass('layui-form-checked');
							if($(this).hasClass('header')){
								$(".layui-form-checkbox").removeClass('layui-form-checked');
							}
						}else{
							$(this).addClass('layui-form-checked');
							if($(this).hasClass('header')){
								$(".layui-form-checkbox").addClass('layui-form-checked');
							}
						}
						
					});
				},
				getData:function  () {
					var obj = $(".layui-form-checked").not('.header');
					var arr=[];
					obj.each(function(index, el) {
						arr.push(obj.eq(index).attr('data-id'));
					});
					return arr;
				}
				
			}
$(function () {

	if(page=='login'){
		if(islogin)window.location.href="/rpa_work/static/page/index.html";
	}else{
		if(!islogin)window.location.href=homedir;
		$('.username').html(username);
	}

    //开启表格多选
    tableCheck.init();
      

    $('.container .left_open i').click(function(event) {
        if($('.left-nav').css('left')=='0px'){
            $('.left-nav').animate({left: '-200px'}, 100);
            $('.page-content').animate({left: '0px'}, 100);
            $('.page-content-bg').hide();
        }else{
            $('.left-nav').animate({left: '0px'}, 100);
            $('.page-content').animate({left: '200px'}, 100);
            if($(window).width()<768){
                $('.page-content-bg').show();
            }
        }
		$(this).toggleClass('layui-icon-spread-left');

    });

    $('.page-content-bg').click(function(event) {
        $('.left-nav').animate({left: '-221px'}, 100);
        $('.page-content').animate({left: '0px'}, 100);
        $(this).hide();
    });

    $('.layui-tab-close').click(function(event) {
        $('.layui-tab-title li').eq(0).find('i').remove();
    });
  
    $(".rightmenu li").click(function () {
       
        if ($(this).attr("data-type") == "closethis") {
            var tabid = $(".layui-tab-title li.layui-this").attr('lay-id');// 获取当前激活的选项卡ID
            tab.tabDelete(tabid);
        } else if ($(this).attr("data-type") == "closeall") {
            tab.closeall();
        }else{
            tab.closeother();
        }
        $('.rightmenu').hide();
    })
    
})


/*弹出层*/
/*
    参数解释：
    title   标题
    url     请求的url
    id      需要操作的数据id
    w       弹出层宽度（缺省调默认值）
    h       弹出层高度（缺省调默认值）
*/
function x_all_show(title,url,w,h){
    if (title == null || title == '') {
        title=false;
    };
    if (url == null || url == '') {
        url="404.html";
    };
    if (w == null || w == '') {
        w=($(window).width()*0.9);
    };
    if (h == null || h == '') {
        h=($(window).height() - 50);
    };
	//window.location.href=url;return false;
    layer.open({
        type: 2,
        area: [w+'px', h +'px'],
        fix: false, //不固定
        maxmin: true,
        shadeClose: false,
        shade:0.4,
        title: title,
        content: url
    });
	
}

/*关闭弹出框口*/
function x_admin_close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}


// 点击空白处关闭右键弹窗
$(document).click(function () {
    $('.rightmenu').hide();
})

/**
* 绑定右键菜单
* @constructor
*/
function CustomRightClick () {

    //屏蔽右键
    $('.layui-tab-title li').on('contextmenu', function () {
        return false;
    })
    $('.layui-tab-title,.layui-tab-title li').click(function () {
        $('.rightmenu').hide();
    });
    $('.layui-tab-title li').on('contextmenu', function (e) {
        var popupmenu = $(".rightmenu");
        l = ($(document).width() - e.clientX) < popupmenu.width() ? (e.clientX - popupmenu.width()) : e.clientX;
        t = ($(document).height() - e.clientY) < popupmenu.height() ? (e.clientY - popupmenu.height()) : e.clientY;
        popupmenu.css({left: l, top: t}).show();
        return false;
    });
}
function checklogin(res){
	if(res.status==1 && res.msg=='SID 已过期/无效')logout(5,'密钥过期,请重新登录');
}
function logout(code=1,msg='退出成功'){
	$.cookie('sid', null,{path: '/',expires: -1});
	$.cookie('user_name', null,{path: '/',expires: -1});
	layer.msg(msg, {
	  icon: code,
	  time: 2000
	}, function(){
	   window.location.href=homedir;
	}); 
}

function getqueryparam (param) {
    var urlParams = new URLSearchParams(window.location.search);
    return urlParams.get(param);
};
function getTodayDate(format) {
    format = format || 'YYYY-MM-DD';

    const date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    let formattedDate = format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day);

    return formattedDate;
}
